import React, { PureComponent } from 'react';
import { ModalC, FormGroup, DATA_ENTRY } from '@/src/components';
import { QuestionCircleOutlined } from '@ant-design/icons';
import { message, Modal, Tooltip } from 'antd';
import { connect } from 'dva';
import FormControl from './formControl';
import FormCreditLine from './formCreditLine';
import style from './index.less';
import moment from 'moment';
import { debounce } from 'lodash';

const { confirm } = Modal;

const formItemLayout = {
  labelCol: {
    span: 5,
  },
  wrapperCol: {
    span: 19,
  },
};
const FormItemLayout1 = {
  labelCol: {
    span: 10,
  },
  wrapperCol: {
    span: 14,
  },
};
@connect(({ creditSettingM, globalM }) => ({
  creditSettingM,
  globalM,
}))
export default class instModal extends PureComponent {
  constructor(props) {
    super(props);
    this.state = {
      timeOutGap: 500,
      loading: false,
      effectiveTime: moment(), // 默认生效时间
      expireTime: moment('2199-12-31'), // 默认失效时间
    };
    this.queryShortName = debounce(this.queryShortName);
  }

  FormGroupRefList = React.createRef();

  initState = () => {
    if (this.props.detailData.effectiveTime) {
      this.setState({
        effectiveTime: moment(this.props.detailData.effectiveTime), // 默认生效时间
        expireTime: moment(this.props.detailData.expireTime), // 默认失效时间
      });
    }
  };

  componentDidMount() {
    this.initState();
  }

  componentWillUnmount() {
    this.cancel();
  }

  // 日期切换
  dateChange = (e, field) => {
    this.setState({
      [field]: e,
    });
  };

  // 交易短名模糊搜索
  queryShortName = (e) => {
    const { dispatch } = this.props;
    const { timeOutGap } = this.state;
    if (e || e === '') {
      this.timer ? clearTimeout(this.timer) : '';
      this.timer = setTimeout(() => {
        dispatch({
          type: 'creditSettingM/getShortName',
          payload: {
            shortName: e,
          },
        });
      }, timeOutGap);
    }
  };

  // 字典转换文字显示
  showValue = (text, arr) => {
    if (!text) return '';
    let newData = '';
    arr.map((item) => {
      if (item.code == text) {
        newData = item.value;
      }
    });
    return newData;
  };

  renderForm = () => {
    const {
      creditSettingM: { portfolioList, shortName },
      detailData,
      type,
      globalM: {
        dataDictionary: {
          COUNTERPARTY_TradeDir = [],
          COUNTERPARTY_Priority = [],
          COUNTERPARTY_CurrencyType = [],
          COUNTERPARTY_MEASUREMENT_METHOD = [],
          COUNTERPARTY_BUSSINESS_TYPE = [],
          COUNTERPARTY_AVAILABLE_STATUS_DICT = [],
        },
      },
    } = this.props;
    const selectList = [
      { value: '0', name: '是' },
      { value: '1', name: '否' },
    ];
    const businessTypeList = COUNTERPARTY_BUSSINESS_TYPE.map((item) => {
      return { value: item.code, title: item.value };
    });

    const arrCreditLines = [];
    const arrControlValues = [];
    if (detailData.creditLines) {
      // 将编辑数据与新增时保持顺序一致
      detailData.creditLines.map((item, index) => {
        arrCreditLines.push({
          key: index + 1,
          startMaturity: item.startMaturity,
          endMaturity: item.endMaturity,
          creditLine: item.creditLine,
        });
      });
    }
    if (detailData.controlValues) {
      detailData.controlValues.map((item, index) => {
        arrControlValues.push({
          key: index + 1,
          controlValue: item.controlValue,
          touchAlarmOperation: item.touchAlarmOperation,
        });
      });
    }
    const controlValues = type == 'create' ? [{ key: 1, controlValue: '0.8', touchAlarmOperation: '1' }] : arrControlValues || [];

    const creditLines =
      type == 'create'
        ? [
            { key: 1, startMaturity: '0M', endMaturity: '1Y', creditLine: '1000' },
            { key: 2, startMaturity: '1Y', endMaturity: '2Y', creditLine: '1000' },
            { key: 3, startMaturity: '2Y', endMaturity: '3Y', creditLine: '1000' },
          ]
        : arrCreditLines || [];
    // 判断是否可编辑
    const isDisabled = type == 'query';
    const isEditDisabled = type != 'query' ? type == 'edit' : true;
    return (
      <>
        <FormGroup
          onRef={(ref) => {
            this.FormGroupRefList.current = ref;
          }}
          autoLayout={false}
          rowNumber={2}
          dataSource={[
            {
              dataIndex: 'explanation',
              title: '授信说明',
              type: DATA_ENTRY.INPUT,
              initialValue: detailData.explanation,
              rules: [{ required: true, message: '请输入授信说明' }],
              formItemLayout,
              disabled: isDisabled,
              max: 30,
              span: 24,
            },
            {
              dataIndex: 'portfolioId',
              type: DATA_ENTRY.PRODUCTGROUP,
              disabled: isDisabled || isEditDisabled,
              initialValue: detailData.portfolioId,
              rules: [{ required: true, message: '请选择产品组' }],
              formItemLayout: FormItemLayout1,
              span: 12,
              positionBody: true,
            },
            {
              dataIndex: 'counterPartyId',
              title: '交易对手短名',
              type: DATA_ENTRY.SELECT,
              dataSource: shortName?.map((item) => ({
                title: item.title,
                value: item.value,
              })),
              initialValue: detailData.counterPartyId,
              rules: [{ required: true, message: '请选择交易对手短名' }],
              span: 12,
              formItemLayout: FormItemLayout1,
              positionBody: true,
              disabled: isDisabled || isEditDisabled,
              showSearch: true,
              filterOption: false,
              onSearch: this.queryShortName,
            },
            {
              dataIndex: 'businessType',
              title: '业务类型',
              type: DATA_ENTRY.SELECT,
              dataSource: businessTypeList?.map((item) => ({
                title: item.title,
                value: item.value,
              })),
              initialValue: detailData.businessType,
              rules: [{ required: true, message: '请选择业务类型' }],
              span: 12,
              formItemLayout: FormItemLayout1,
              positionBody: true,
              disabled: isDisabled || isEditDisabled,
              isMultiple: true,
              maxTagCount: 0,
            },
            {
              dataIndex: 'measurementMethod',
              title: '额度计量方法',
              type: DATA_ENTRY.SELECT,
              dataSource: COUNTERPARTY_MEASUREMENT_METHOD?.map((item) => ({
                title: item.value,
                value: item.code,
              })),
              initialValue: detailData.measurementMethod,
              rules: [{ required: true, message: '请选择额度计量方法' }],
              span: 12,
              formItemLayout: FormItemLayout1,
              positionBody: true,
              disabled: isDisabled || isEditDisabled,
            },
            {
              dataIndex: 'isParentOccupated',
              title: '是否占用母机构额度',
              type: DATA_ENTRY.SELECT,
              dataSource: selectList?.map((item) => ({
                title: item.name,
                value: item.value,
              })),
              initialValue: detailData.isParentOccupated || '0',
              rules: [{ required: true, message: '请选择' }],
              span: 12,
              formItemLayout: FormItemLayout1,
              positionBody: true,
              disabled: isDisabled || isEditDisabled,
            },
            {
              dataIndex: 'containsExanteTransaction',
              title: '是否包含事前交易',
              type: DATA_ENTRY.SELECT,
              dataSource: selectList?.map((item) => ({
                title: item.name,
                value: item.value,
              })),
              initialValue: detailData.containsExanteTransaction || '0',
              rules: [{ required: true, message: '请选择' }],
              span: 12,
              formItemLayout: FormItemLayout1,
              positionBody: true,
              disabled: isDisabled || isEditDisabled,
            },
            {
              dataIndex: 'tradeDir',
              title: '交易方向',
              type: DATA_ENTRY.SELECT,
              dataSource: COUNTERPARTY_TradeDir?.map((item) => ({
                title: item.value,
                value: item.code,
              })),
              initialValue: detailData.tradeDir || '1',
              rules: [{ required: true, message: '请选择交易方向' }],
              span: 12,
              formItemLayout: FormItemLayout1,
              positionBody: true,
              disabled: isDisabled || isEditDisabled,
            },
            {
              dataIndex: 'currencyType',
              title: '额度币种',
              type: DATA_ENTRY.SELECT,
              dataSource: COUNTERPARTY_CurrencyType?.map((item) => ({
                title: item.value,
                value: item.code,
              })),
              initialValue: detailData.currencyType || '1',
              rules: [{ required: true, message: '请选择额度币种' }],
              span: 12,
              formItemLayout: FormItemLayout1,
              positionBody: true,
              disabled: isDisabled || isEditDisabled,
            },
            {
              dataIndex: 'effectiveTime',
              type: DATA_ENTRY.SINGLEDATE,
              title: '生效时间',
              initialValue: detailData.effectiveTime ? moment(detailData.effectiveTime) : moment(),
              rules: [{ required: true, message: '请选择生效时间' }],
              disabled: isDisabled,
              span: 12,
              formItemLayout: FormItemLayout1,
              onChange: (e) => {
                this.dateChange(e, 'effectiveTime');
              },
              disableBegin: moment().subtract(1, 'days'),
            },
            {
              dataIndex: 'expireTime',
              type: DATA_ENTRY.SINGLEDATE,
              title: '失效时间',
              initialValue: detailData.expireTime ? moment(detailData.expireTime) : moment('2199-12-31'),
              rules: [{ required: true, message: '请选择失效时间' }],
              disabled: isDisabled,
              span: 12,
              formItemLayout: FormItemLayout1,
              onChange: (e) => {
                this.dateChange(e, 'expireTime');
              },
              disableBegin: moment(),
            },
            {
              dataIndex: 'priority',
              title: '优先级',
              type: DATA_ENTRY.SELECT,
              dataSource: COUNTERPARTY_Priority?.map((item) => ({
                title: item.value,
                value: item.code,
              })),
              initialValue: detailData.priority || '5',
              rules: [{ required: true, message: '请选择优先级' }],
              span: 12,
              formItemLayout: FormItemLayout1,
              positionBody: true,
              disabled: isDisabled,
            },
            {
              style: { height: 'unset' },
              dataIndex: 'controlValue',
              title: '触警操作设置',
              type: DATA_ENTRY.FORMOTHERC,
              initialValue: controlValues,
              rules: [{ required: true }, { validator: this.checkControlValue }],
              Children: <FormControl disabled={isDisabled} />,
              formItemLayout,
              span: 24,
            },
            {
              style: { height: 'unset' },
              dataIndex: 'creditLine',
              title: (
                <span>
                  {'授信额度设置 '}&nbsp;
                  <Tooltip title={'剩余期限单位Y表示年,M表示月,区间段从小到大填写'}>
                    <QuestionCircleOutlined style={{ fontSize: '16px' }} />
                  </Tooltip>
                </span>
              ),
              type: DATA_ENTRY.FORMOTHERC,
              initialValue: creditLines,
              rules: [{ required: true }, { validator: this.checkCreditLine }],
              Children: <FormCreditLine disabled={isDisabled} />,
              formItemLayout,
              span: 24,
            },
            {
              hide: !(type != 'create'),
              dataIndex: 'status',
              title: '可用状态',
              type: DATA_ENTRY.INPUT,
              initialValue: this.showValue(detailData.status, COUNTERPARTY_AVAILABLE_STATUS_DICT),
              formItemLayout: FormItemLayout1,
              disabled: true,
              span: 12,
            },
            {
              hide: !(type != 'create'),
              dataIndex: 'creator',
              title: '创建人',
              type: DATA_ENTRY.INPUT,
              initialValue: detailData.creator,
              formItemLayout: FormItemLayout1,
              disabled: true,
              span: 12,
            },
            {
              hide: !(type != 'create'),
              dataIndex: 'createTime',
              type: DATA_ENTRY.SINGLEDATE,
              title: '创建时间',
              initialValue: detailData.createTime ? moment(detailData.createTime) : null,
              disabled: true,
              span: 12,
              formItemLayout: FormItemLayout1,
            },
            {
              hide: !(type != 'create'),
              dataIndex: 'modifier',
              title: '修改人',
              type: DATA_ENTRY.INPUT,
              initialValue: detailData.modifier,
              formItemLayout: FormItemLayout1,
              disabled: true,
              span: 12,
            },
            {
              hide: !(type != 'create'),
              dataIndex: 'modificationTime',
              type: DATA_ENTRY.SINGLEDATE,
              title: '上一次修改时间',
              initialValue: detailData.modificationTime ? moment(detailData.modificationTime) : null,
              disabled: true,
              span: 12,
              formItemLayout: FormItemLayout1,
            },
          ]}
        />
        {/* <Form> */}
        {/*  <Row> */}
        {/*    <Col span={24}> */}
        {/*      <FormItem label='授信说明' {...formItemLayout}> */}
        {/*        {getFieldDecorator('explanation', { */}
        {/*          initialValue: detailData.explanation, */}
        {/*          rules: [{ required: true, message: '请输入授信说明' }], */}
        {/*        })(<Input disabled={isDisabled} style={{ width: '100%' }} maxLength={30} />)} */}
        {/*      </FormItem> */}
        {/*    </Col> */}
        {/*  </Row> */}
        {/*  <Row> */}
        {/*    <Col span={12}> */}
        {/*      <ProductGroupC */}
        {/*        form={this.props.form} */}
        {/*        dataIndex={'portfolioId'} */}
        {/*        initialValue={detailData.portfolioId} */}
        {/*        formItemLayout={FormItemLayout1} */}
        {/*        rules={[{ required: true, message: '请选择产品组' }]} */}
        {/*        disabled={isDisabled || isEditDisabled} */}
        {/*      /> */}
        {/*    </Col> */}
        {/*    <Col span={12}> */}
        {/*      <FormItem label='交易对手短名' {...FormItemLayout1}> */}
        {/*        {getFieldDecorator('counterPartyId', { */}
        {/*          initialValue: detailData.counterPartyId, */}
        {/*          rules: [{ required: true, message: '请选择交易对手短名' }], */}
        {/*        })( */}
        {/*          <Select style={{ width: '100%' }} showSearch={true} filterOption={false} onSearch={this.queryShortName} disabled={isDisabled || isEditDisabled}> */}
        {/*            {shortName.map((item, i) => ( */}
        {/*              <Option value={item.value} key={i}> */}
        {/*                {item.title} */}
        {/*              </Option> */}
        {/*            ))} */}
        {/*          </Select>, */}
        {/*        )} */}
        {/*      </FormItem> */}
        {/*    </Col> */}
        {/*  </Row> */}
        {/*  <Row> */}
        {/*    <Col span={12}> */}
        {/*      <FormItem label='业务类型' {...FormItemLayout1}> */}
        {/*        {getFieldDecorator('businessType', { */}
        {/*          initialValue: detailData.businessType, */}
        {/*          rules: [{ required: true, message: '请选择业务类型' }], */}
        {/*        })( */}
        {/*          <Select */}
        {/*            style={{ width: '100%' }} */}
        {/*            disabled={isDisabled || isEditDisabled} */}
        {/*            maxTagPlaceholder={(omittedValues) => { */}
        {/*              return <span title={this.businessTypeShow(omittedValues)}>共选择{omittedValues.length}项</span>; */}
        {/*            }} */}
        {/*            mode={'multiple'} */}
        {/*            maxTagCount={0} */}
        {/*          > */}
        {/*            {businessTypeList && */}
        {/*              businessTypeList.map((item, i) => ( */}
        {/*                <Option value={item.value} key={i}> */}
        {/*                  {item.title} */}
        {/*                </Option> */}
        {/*              ))} */}
        {/*          </Select>, */}
        {/*        )} */}
        {/*      </FormItem> */}
        {/*    </Col> */}
        {/*    <Col span={12}> */}
        {/*      <FormItem label='额度计量方法' {...FormItemLayout1}> */}
        {/*        {getFieldDecorator('measurementMethod', { */}
        {/*          initialValue: detailData.measurementMethod, */}
        {/*          rules: [{ required: true, message: '请选择额度计量方法' }], */}
        {/*        })( */}
        {/*          <Select disabled={isDisabled || isEditDisabled} style={{ width: '100%' }}> */}
        {/*            {COUNTERPARTY_MEASUREMENT_METHOD.map((item, i) => ( */}
        {/*              <Option value={item.code} key={i}> */}
        {/*                {item.value} */}
        {/*              </Option> */}
        {/*            ))} */}
        {/*          </Select>, */}
        {/*        )} */}
        {/*      </FormItem> */}
        {/*    </Col> */}
        {/*  </Row> */}
        {/*  <Row> */}
        {/*    <Col span={12}> */}
        {/*      <FormItem label='是否占用母机构额度' {...FormItemLayout1}> */}
        {/*        {getFieldDecorator('isParentOccupated', { */}
        {/*          initialValue: detailData.isParentOccupated || '0', */}
        {/*          rules: [{ required: true, message: '请选择' }], */}
        {/*        })( */}
        {/*          <Select disabled={isDisabled || isEditDisabled} style={{ width: '100%' }}> */}
        {/*            {optionChild} */}
        {/*          </Select>, */}
        {/*        )} */}
        {/*      </FormItem> */}
        {/*    </Col> */}
        {/*    <Col span={12}> */}
        {/*      <FormItem label='是否包含事前交易' {...FormItemLayout1}> */}
        {/*        {getFieldDecorator('containsExanteTransaction', { */}
        {/*          initialValue: detailData.containsExanteTransaction || '0', */}
        {/*          rules: [{ required: true, message: '请选择' }], */}
        {/*        })( */}
        {/*          <Select disabled={isDisabled || isEditDisabled} style={{ width: '100%' }}> */}
        {/*            {optionChild} */}
        {/*          </Select>, */}
        {/*        )} */}
        {/*      </FormItem> */}
        {/*    </Col> */}
        {/*  </Row> */}
        {/*  <Row> */}
        {/*    <Col span={12}> */}
        {/*      <FormItem label='交易方向' {...FormItemLayout1}> */}
        {/*        {getFieldDecorator('tradeDir', { */}
        {/*          initialValue: detailData.tradeDir || '1', */}
        {/*          rules: [{ required: true, message: '请选择交易方向' }], */}
        {/*        })( */}
        {/*          <Select disabled={isDisabled || isEditDisabled} style={{ width: '100%' }}> */}
        {/*            {COUNTERPARTY_TradeDir.map((item, i) => ( */}
        {/*              <Option value={item.code} key={i}> */}
        {/*                {item.value} */}
        {/*              </Option> */}
        {/*            ))} */}
        {/*          </Select>, */}
        {/*        )} */}
        {/*      </FormItem> */}
        {/*    </Col> */}
        {/*    <Col span={12}> */}
        {/*      <FormItem label='额度币种' {...FormItemLayout1}> */}
        {/*        {getFieldDecorator('currencyType', { */}
        {/*          initialValue: detailData.currencyType || '1', */}
        {/*          rules: [{ required: true, message: '请选择额度币种' }], */}
        {/*        })( */}
        {/*          <Select disabled={isDisabled || isEditDisabled} style={{ width: '100%' }}> */}
        {/*            {COUNTERPARTY_CurrencyType.map((item, i) => ( */}
        {/*              <Option value={item.code} key={i}> */}
        {/*                {item.value} */}
        {/*              </Option> */}
        {/*            ))} */}
        {/*          </Select>, */}
        {/*        )} */}
        {/*      </FormItem> */}
        {/*    </Col> */}
        {/*  </Row> */}
        {/*  <Row> */}
        {/*    <Col span={12}> */}
        {/*      <FormItem label='生效时间' {...FormItemLayout1}> */}
        {/*        {getFieldDecorator('effectiveTime', { */}
        {/*          initialValue: detailData.effectiveTime ? moment(detailData.effectiveTime) : moment(), */}
        {/*          rules: [{ required: true, message: '请选择生效时间' }], */}
        {/*        })( */}
        {/*          <DatePicker */}
        {/*            disabled={isDisabled} */}
        {/*            onChange={(e) => { */}
        {/*              this.dateChange(e, 'effectiveTime'); */}
        {/*            }} */}
        {/*            disabledDate={(e) => this.disabledStartDate(e, 'expireTime')} */}
        {/*            placeholder='请选择' */}
        {/*            style={{ width: '100%' }} */}
        {/*          />, */}
        {/*        )} */}
        {/*      </FormItem> */}
        {/*    </Col> */}
        {/*    <Col span={12}> */}
        {/*      <FormItem label='失效时间' {...FormItemLayout1}> */}
        {/*        {getFieldDecorator('expireTime', { */}
        {/*          initialValue: detailData.expireTime ? moment(detailData.expireTime) : moment('2199-12-31'), */}
        {/*          rules: [{ required: true, message: '请选择失效时间' }], */}
        {/*        })( */}
        {/*          <DatePicker */}
        {/*            disabled={isDisabled} */}
        {/*            onChange={(e) => { */}
        {/*              this.dateChange(e, 'expireTime'); */}
        {/*            }} */}
        {/*            disabledDate={(e) => this.disabledEndDate(e, 'effectiveTime')} */}
        {/*            placeholder='请选择' */}
        {/*            style={{ width: '100%' }} */}
        {/*          />, */}
        {/*        )} */}
        {/*      </FormItem> */}
        {/*    </Col> */}
        {/*  </Row> */}
        {/*  <Row> */}
        {/*    <Col span={12}> */}
        {/*      <FormItem label='优先级' {...FormItemLayout1}> */}
        {/*        {getFieldDecorator('priority', { */}
        {/*          initialValue: detailData.priority || '5', */}
        {/*          rules: [{ required: true, message: '请选择优先级' }], */}
        {/*        })( */}
        {/*          <Select disabled={isDisabled} style={{ width: '100%' }}> */}
        {/*            {COUNTERPARTY_Priority.map((item, i) => ( */}
        {/*              <Option value={item.code} key={i}> */}
        {/*                {item.value} */}
        {/*              </Option> */}
        {/*            ))} */}
        {/*          </Select>, */}
        {/*        )} */}
        {/*      </FormItem> */}
        {/*    </Col> */}
        {/*  </Row> */}
        {/*  <Row> */}
        {/*    <Col span={24} className={style.controlError}> */}
        {/*      <Form.Item label='触警操作设置' {...formItemLayout} style={{ marginBottom: '0px!important' }}> */}
        {/*        {getFieldDecorator('controlValue', { */}
        {/*          rules: [ */}
        {/*            { required: true }, */}
        {/*            { */}
        {/*              validator: this.checkControlValue, */}
        {/*            }, */}
        {/*          ], */}
        {/*          initialValue: controlValues, */}
        {/*        })(<FormControl disabled={isDisabled} />)} */}
        {/*      </Form.Item> */}
        {/*    </Col> */}
        {/*  </Row> */}
        {/*  <Row> */}
        {/*    <Col span={24} className={style.controlError}> */}
        {/*      <Form.Item */}
        {/*        label={ */}
        {/*          <span> */}
        {/*            {'授信额度设置 '}&nbsp; */}
        {/*            <Tooltip title={'剩余期限单位Y表示年,M表示月,区间段从小到大填写'}> */}
        {/*              <QuestionCircleOutlined style={{ fontSize: '16px' }} /> */}
        {/*            </Tooltip> */}
        {/*          </span> */}
        {/*        } */}
        {/*        {...formItemLayout} */}
        {/*        style={{ marginBottom: '0' }} */}
        {/*      > */}
        {/*        {getFieldDecorator('creditLine', { */}
        {/*          rules: [ */}
        {/*            { required: true }, */}
        {/*            { */}
        {/*              validator: this.checkCreditLine, */}
        {/*            }, */}
        {/*          ], */}
        {/*          initialValue: creditLines, */}
        {/*        })(<FormCreditLine disabled={isDisabled} />)} */}
        {/*      </Form.Item> */}
        {/*    </Col> */}
        {/*  </Row> */}
        {/*  {type != 'create' ? ( */}
        {/*    <Row className={style.modifyInfo}> */}
        {/*      <Col span={12}> */}
        {/*        <FormItem label='可用状态' {...FormItemLayout1}> */}
        {/*          {getFieldDecorator('status', { */}
        {/*            initialValue: this.showValue(detailData.status, COUNTERPARTY_AVAILABLE_STATUS_DICT), */}
        {/*          })(<Input disabled={true} style={{ width: '100%' }} />)} */}
        {/*        </FormItem> */}
        {/*      </Col> */}
        {/*      <Col span={12}> */}
        {/*        <FormItem label='创建人' {...FormItemLayout1}> */}
        {/*          {getFieldDecorator('creator', { */}
        {/*            initialValue: detailData.creator, */}
        {/*          })(<Input disabled={true} style={{ width: '100%' }} />)} */}
        {/*        </FormItem> */}
        {/*      </Col> */}
        {/*      <Col span={12}> */}
        {/*        <FormItem label='创建时间' {...FormItemLayout1}> */}
        {/*          {getFieldDecorator('createTime', { */}
        {/*            initialValue: detailData.createTime ? moment(detailData.createTime) : null, */}
        {/*          })(<DatePicker disabled={true} style={{ width: '100%' }} />)} */}
        {/*        </FormItem> */}
        {/*      </Col> */}
        {/*      <Col span={12}> */}
        {/*        <FormItem label='修改人' {...FormItemLayout1}> */}
        {/*          {getFieldDecorator('modifier', { */}
        {/*            initialValue: detailData.modifier, */}
        {/*          })(<Input disabled={true} style={{ width: '100%' }} />)} */}
        {/*        </FormItem> */}
        {/*      </Col> */}
        {/*      <Col span={12}> */}
        {/*        <FormItem label='上一次修改时间' {...FormItemLayout1}> */}
        {/*          {getFieldDecorator('modificationTime', { */}
        {/*            initialValue: detailData.modificationTime ? moment(detailData.modificationTime) : null, */}
        {/*          })(<DatePicker disabled={true} style={{ width: '100%' }} />)} */}
        {/*        </FormItem> */}
        {/*      </Col> */}
        {/*    </Row> */}
        {/*  ) : null} */}
        {/* </Form> */}
      </>
    );
  };

  isRepeat = (arr) => {
    // 判断数组里是否有相同的元素
    const hash = {};
    for (const i in arr) {
      if (hash[arr[i]]) {
        return true;
      }
      hash[arr[i]] = true;
    }
    return false;
  };

  // 授信设置校验
  checkCreditLine = (rule, value, callback) => {
    const arr = [];
    let flag; // 是否填写完整
    const regTest = []; // 数字格式是否正确
    value.map((item) => {
      for (const i in item) {
        if (i == 'endMaturity' || i == 'startMaturity') {
          if (item[i] && !/^([0-9]*)[M|Y]$/.test(item[i]) && item[i] != '全部') {
            regTest.push(1);
            return;
          }
          if (item.endMaturity && item.startMaturity && item.startMaturity != '全部') {
            const end = Number(item.endMaturity.substr(0, item.endMaturity.length - 1));
            const endUnit = item.endMaturity.slice(item.endMaturity.length - 1); // 单位
            const start = Number(item.startMaturity.substr(0, item.startMaturity.length - 1));
            const startUnit = item.startMaturity.slice(item.startMaturity.length - 1); // 单位
            if (endUnit == startUnit && (end < start || end == start)) {
              callback('起始必须小于终止');
              return;
            } else if (endUnit != startUnit) {
              // 12M=1Y
              if (endUnit == 'M') {
                if (end / 12 < start || end / 12 == start) {
                  callback('起始必须小于终止');
                  return;
                }
              } else {
                if (end < start / 12 || end == start / 12) {
                  callback('起始必须小于终止');
                  return;
                }
              }
            }
          }
        }
        if (i == 'creditLine') {
          if (item[i] && !/^([1-9][0-9]*)+(\.\d+)?$/.test(item[i])) regTest.push(1);
        }
      }
    });
    if (regTest.length) {
      callback('请输入正确的格式');
      return;
    }

    flag = value.some((item) => {
      return Object.values(item).some((x) => {
        return !x;
      });
    });
    if (flag) {
      callback('请设置完整的授信额度');
    }
    value.map((item) => {
      // 判断是否有重复条数
      const temp = Object.values(item);
      temp.shift();
      arr.push(temp.join(','));
    });

    if (this.isRepeat(arr)) {
      callback('授信额度设置不能重复');
    } else {
      callback();
    }
  };

  // 触警设置校验
  checkControlValue = (rule, value, callback) => {
    const arr = [];
    const arr1 = [];
    let flag; // 是否填写完整
    value.map((item) => {
      // 判断是否有重复条数
      const temp = Object.values(item);
      arr.push(temp.pop());
      arr1.push(temp.pop());
    });
    flag = value.some((item) => {
      return Object.values(item).some((x) => {
        return !x;
      });
    });
    if (flag) {
      callback('请设置完整的触警操作');
    } else if (this.isRepeat(arr)) {
      callback('警告、审批、禁止每种只能有一条');
    } else if (this.isRepeat(arr1)) {
      callback('每种类型对应的控制值不能一样');
    } else {
      callback();
    }
  };

  // 点击确定
  confirmRevise = async (e) => {
    e.preventDefault();
    const { type, detailData = {} } = this.props;
    const fieldsValue = await this.FormGroupRefList.current?.getFormGroupData();
    this.setState({
      loading: true,
    });
    const url = type == 'edit' ? 'creditSettingM/updateCredit' : 'creditSettingM/addCredit';
    let params = {};
    if (this.props.from) {
      // 审批流更新需要加一些参数
      params = {
        type: this.props.from,
        pid: detailData.pid,
        createTime: detailData.createTime,
        creator: detailData.creator,
        status: detailData.status,
      };
    }
    this.props.dispatch({
      type: url,
      payload: {
        id: detailData.id,
        ...fieldsValue,
        expireTime: moment(fieldsValue.expireTime).format('YYYY-MM-DD'),
        effectiveTime: moment(fieldsValue.effectiveTime).format('YYYY-MM-DD'),
        ...params,
      },
      callback: (msg) => {
        this.setState({
          loading: false,
        });
        if (msg) {
          message.success(msg);
          this.cancel();
          this.props.getTableDataNew();
        }
      },
    });
  };

  // 点击取消
  cancel = (currentType) => {
    const { type } = this.props;
    const form = this.FormGroupRefList.current?.formRef.current;
    if (currentType == 'close' && type != 'query') {
      // 新增和编辑的情况下 点击关闭 需要二次确认
      confirm({
        title: '是否确定不保存已修改内容?',
        okText: '确定',
        cancelText: '取消',
        onOk: () => {
          form.resetFields();
          this.props.setVisible(false);
        },
      });
    } else {
      form.resetFields();
      // 审批页面关闭路由 不进行调用
      if (!(this.props.type == 'query' && this.props.id)) this.props.setVisible(false);
    }
  };

  render() {
    const { titleModal } = this.props;
    const { type } = this.props;

    return (
      <div>
        {type == 'query' && this.props.id ? (
          <div className={style.creditModal} style={{ width: '68%' }}>
            {this.renderForm()}
          </div>
        ) : (
          <ModalC
            title={titleModal}
            visible={this.props.visible}
            onOk={this.confirmRevise}
            onCancel={() => this.cancel('close')}
            className={style.creditModal}
            confirmLoading={this.state.loading == true}
            footer={type == 'query' ? null : undefined}
          >
            <div>{this.renderForm()}</div>
          </ModalC>
        )}
      </div>
    );
  }
}
